package cheng.rex;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import java.io.Reader;
import java.util.Iterator;
import java.util.List;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class First {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			String resource = "config/config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
	        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
	        SqlSessionFactory factory = builder.build(inputStream);
	        
	        SqlSession session = factory.openSession();
	        UserDao userDao = session.getMapper(UserDao.class);
	        
	        User user = new User();
	        user.setUserName("hongye");
	        user.setPassword("123456");
	        user.setComment("comment");
	        
	        userDao.insert(user);
	        System.out.println("record No.："+userDao.countAll());
	        
	        List<User> users = userDao.selectAll();
	        Iterator<User> iter = users.iterator();
	        while(iter.hasNext()){
	            User u = iter.next();
	            System.out.println("user name："+u.getUserName()+"password："+u.getPassword());
	        }
	        
	        user.setComment("comment");
	        userDao.update(user);
	        User u = userDao.findByUserName("hongye");
	        System.out.println(u.getComment());
	        
	        userDao.delete("hongye");
	        System.out.println("record No.："+userDao.countAll());
	        
	        session.commit();
	        session.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
